package br.com.rampage.controller.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.rampage.model.bean.Carteira;
import br.com.rampage.model.bean.Dependente;
import br.com.rampage.model.bean.Paciente;
import br.com.rampage.utils.AppUtils;

@WebServlet("/PreviewToApplicationServlet")
public class PreviewToApplicationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public PreviewToApplicationServlet() {
        super();
    }

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispacher = request.getRequestDispatcher("aplicacao.jsp");
		
		Paciente p = AppUtils.getInstace().getPacienteSelecionado();
		String nameSelected = request.getParameter("name");
		Carteira carteira = null;
		
		if (p.getNome().equals(nameSelected)) {
			carteira = p.getCarteira();
		
		} else {
			for (Dependente dependente : p.getDepedentes()) {
				if(dependente.getNome().equals(nameSelected)) {
					carteira = dependente.getCarteira();
				}
			}
		}
		
		request.setAttribute("carteira", carteira);
		dispacher.forward(request, response);
	}

}
